package com.example.officialwebsiteadmin.controller;


import com.example.officialwebsiteadmin.common.Result;
import com.example.officialwebsiteadmin.common.WangEditorVO;
import com.example.officialwebsiteadmin.model.entity.Articles;
import com.example.officialwebsiteadmin.model.service.ArticlesServices;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RequestMapping("/wanxin/as")
@RestController
public class ArticlesController {

    @Autowired
    ArticlesServices articlesServices;

    @GetMapping()
    public List<Articles> ArticlesList(){
//        System.out.println(articlesServices.ArticlesList());
        return articlesServices.ArticlesList();
    }

    @GetMapping("/page")
    public List<Articles> ArticlesPaging(Integer pagenum,Integer pagesize){
        return articlesServices.ArticlesPaging(pagenum,pagesize);
    }

    @DeleteMapping()
    public Boolean ArticlesDel(@RequestParam Integer id){
        return articlesServices.ArticlesDel(id);
    }

//    @PutMapping()
//    public Boolean ArticlesPut(@RequestBody Object data){
//        return articlesServices.ArticlesPut(data);
//    }

    @PostMapping()
    public Boolean ArticlesAdd(@RequestBody Object data){
        // 将请求体的数据映射为一个 Map 对象
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> dataMap = objectMapper.convertValue(data, Map.class);

        try {
            dataMap.get("id").toString();
            return articlesServices.ArticlesPut(data);
        } catch (Exception e) {
            e.printStackTrace();
            return articlesServices.ArticlesAdd(data);
        }
    }
    @PostMapping("/upload")
    public WangEditorVO uploadimg(MultipartFile wangeditorUploadedImage){
        System.out.println(wangeditorUploadedImage);
        String filePath = "C:\\Users\\xingjisen\\Desktop\\";
        try {
            return WangEditorVO.success(filePath + articlesServices.uploadimg(wangeditorUploadedImage,filePath));
        } catch (Exception e) {
            e.printStackTrace();
            return WangEditorVO.error(0,filePath + articlesServices.uploadimg(wangeditorUploadedImage,filePath));
        }
    }

    @GetMapping("/code")
    public Object Code(@RequestParam("codeData") String codeData){
        return articlesServices.Code(codeData);
    }
    //存储表单信息
    @PostMapping("/submsg")
    public Boolean subMsg(@RequestBody Object codeData){
        return articlesServices.subMsg(codeData);
    }

}
